/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cheetah.youbet;

import org.cheetah.youbet.core.GetScores;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.cheetah.youbet.beans.Avvenimento;
import org.cheetah.youbet.beans.Palinsesto;
import org.cheetah.youbet.download.PianetaScommesseDownload;

/**
 *
 * @author edoardo
 */
@Deprecated
public class CheckScores {

    public static void main(String[] args) throws Exception {
        if (args == null || args.length < 2) {
            throw new Exception("Specificare parametri: [/path/to/bolletta] [data del risultato nel fomato aa_mm_gg]");
        }
        Map<String, Palinsesto> risultati = PianetaScommesseDownload.getRisultatiPdf(args[1],true);
        Workbook wb = WorkbookFactory.create(new FileInputStream(args[0]));
        Sheet sheet = wb.getSheetAt(0);
        Iterator<Row> rows = sheet.iterator();
        //salto la prima riga che è l'intestazione
        rows.next();
        while (rows.hasNext()) {
            Row row = rows.next();
            String palinsesto = row.getCell(0).getStringCellValue();
            palinsesto = palinsesto.substring(palinsesto.indexOf(".") + 1).trim();
            Palinsesto p = risultati.get(palinsesto);
            String avv = row.getCell(2).getStringCellValue().trim();
            if (p != null) {
                for (Avvenimento avvenimento : p.getAvvenimentos()) {
                    if (avv.equals(avvenimento.getNumero().trim())) {
                        Cell c = row.createCell(8);
                        c.setCellValue(avvenimento.getRisultatoSecondoTempo());
                        Cell pronostico = row.getCell(5);
                        String value = pronostico.getStringCellValue();
                        if (isCorrect(value, avvenimento.getRisultatoSecondoTempo())) {
                            CellStyle style = wb.createCellStyle();
                            style.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
                            style.setFillPattern(CellStyle.BIG_SPOTS);
                            Iterator<Cell> cells = row.iterator();
                            while (cells.hasNext()) {
                                Cell cell = cells.next();
                                cell.setCellStyle(style);
                            }
                        }
                    }
                }
            }
        }
        FileOutputStream fileOut = new FileOutputStream(args[0]);
        wb.write(fileOut);
        fileOut.close();
    }

    private static boolean isCorrect(String value, String ris) {
        int home = Integer.parseInt(ris.substring(0, ris.indexOf("-")));
        int away = Integer.parseInt(ris.substring(ris.indexOf("-") + 1));
        if (value.trim().equalsIgnoreCase("1")) {
            return home > away;
        }
        if (value.trim().equalsIgnoreCase("x")) {
            return home == away;

        }
        if (value.trim().equalsIgnoreCase("2")) {
            return home < away;

        }
        if (value.trim().equalsIgnoreCase("1X")) {
            return home >= away;

        }
        if (value.trim().equalsIgnoreCase("12")) {
            return home != away;

        }
        if (value.trim().equalsIgnoreCase("X2")) {
            return home <= away;

        }
        return false;
    }

}
